package com.example.demo.utils;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author liulanhua
 * @date
 */
@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    public Result(Integer code, String msg) {
        super();
        this.msg = msg;
        this.code = code;
    }

    public Result(boolean success, T data) {
        super();
        this.success = success;
        this.data = data;
    }

    public Result(boolean success, String msg) {
        super();
        this.success = success;
        this.msg = msg;
    }

    public Result(Integer code, T data) {
        super();
        this.code = code;
        this.data = data;
    }

    public Result(boolean success, Integer code, String msg) {
        super();
        this.msg = msg;
        this.code = code;
        this.success = success;
    }

    public Result(boolean success, T data, String msg) {
        super();
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    public Result(Integer code, T data, String msg) {
        super();
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public Result(boolean success, Integer code, T data, String msg) {
        super();
        this.success = success;
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    /**
     * 初始失败方法
     *
     * @see [类、类#方法、类#成员]
     */
    public Result<T> invokeFail() {
        this.success = Boolean.FALSE;
        this.data = null;
        this.code = HttpStatus.BAD_REQUEST.value();
        this.msg = "操作失败";
        return this;
    }

    public Result<T> invokeFail(String msg) {
        this.success = Boolean.FALSE;
        this.data = null;
        this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.msg = msg;
        return this;
    }

    public Result<T> invokeFail(Integer code, String msg) {
        this.success = Boolean.FALSE;
        this.data = null;
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> invokeFail(T data, Integer code, String msg) {
        this.success = Boolean.FALSE;
        this.data = data;
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> invokeSuccess() {
        this.success = Boolean.TRUE;
        this.code = HttpStatus.OK.value();
        this.msg = "操作成功";
        return this;
    }

    public Result<T> invokeSuccess(T data) {
        this.success = Boolean.TRUE;
        this.data = data;
        this.msg = null;
        this.code = HttpStatus.OK.value();
        return this;
    }

    public Result<T> invokeSuccess(T data, String msg) {
        this.success = Boolean.TRUE;
        this.data = data;
        this.msg = msg;
        this.code = HttpStatus.OK.value();
        return this;
    }

    public Result<T> invokeSuccess(T data, Integer code, String msg) {
        this.success = Boolean.TRUE;
        this.data = data;
        this.msg = msg;
        this.code = code;
        return this;
    }

}
